#!/usr/bin/env sh

export HADOOP_CLASSPATH=`hadoop classpath`

wait_for() {
    echo 正在等待 $1 监听 $2 启动情况...
    while ! nc -z $1 $2; do echo 正在等待...; sleep 1s; done
}

start_hdfs_namenode() {
    if [ ! -f /tmp/namenode-formated ];then
        ${HADOOP_HOME}/bin/hdfs namenode -format >/tmp/namenode-formated
    fi
    ${HADOOP_HOME}/bin/hdfs --loglevel INFO --daemon start namenode
    tail -f ${HADOOP_HOME}/logs/*namenode*.log
}

start_hdfs_datanode() {
    wait_for $1 $2
    ${HADOOP_HOME}/bin/hdfs --loglevel INFO --daemon start datanode
    tail -f ${HADOOP_HOME}/logs/*datanode*.log
}

start_yarn_resourcemanager() {
    ${HADOOP_HOME}/bin/yarn --loglevel INFO --daemon start resourcemanager
    tail -f ${HADOOP_HOME}/logs/*resourcemanager*.log
}

start_yarn_nodemanager() {
    wait_for $1 $2
    ${HADOOP_HOME}/bin/yarn --loglevel INFO --daemon start nodemanager
    tail -f ${HADOOP_HOME}/logs/*nodemanager*.log
}

start_yarn_proxyserver() {
    wait_for $1 $2
    ${HADOOP_HOME}/bin/yarn --loglevel INFO --daemon start proxyserver
    tail -f ${HADOOP_HOME}/logs/*proxyserver*.log
}

start_mr_historyserver() {
    wait_for $1 $2
    ${HADOOP_HOME}/bin/mapred --loglevel INFO  --daemon  start historyserver
    tail -f ${HADOOP_HOME}/logs/*historyserver*.log
}

start_hive_metastore() {
    wait_for $1 $2
    if [ ! -f ${HIVE_HOME}/formated ];then
        schematool -initSchema -dbType mysql --verbose >  ${HIVE_HOME}/formated
    fi
    $HIVE_HOME/bin/hive --service metastore

}

start_hive_hiveserver2() {
    wait_for $1 $2
    $HIVE_HOME/bin/hive --service hiveserver2
}

start_hive_svc() {
    wait_for $1 $2
    if [ ! -f ${HIVE_HOME}/formated ];then
        sleep 5s
        schematool -initSchema -dbType mysql --verbose >  ${HIVE_HOME}/formated
    fi
    $HIVE_HOME/bin/hive --service metastore &
    $HIVE_HOME/bin/hive --service hiveserver2
}

start_flink_yarn_session() {
    wait_for $1 $2

    export HADOOP_CLASSPATH=`hadoop classpath`
    $FLINK_HOME/bin/yarn-session.sh -nm yarn_session
}

start_dinky_flink17() {
    wait_for $1 $2

    $DINKY_HOME/auto.sh startOnPending 1.17
}

start_dinky_flink18() {
    wait_for $1 $2

    $DINKY_HOME/auto.sh startOnPending 1.18
}

start_dinky_flink() {
    wait_for $1 $2

    $DINKY_HOME/auto.sh startOnPending
}

case $1 in
    hadoop-hdfs-nn)
        start_hdfs_namenode
        ;;
    hadoop-hdfs-dn)
        start_hdfs_datanode $2 $3
        ;;
    hadoop-yarn-rm)
        start_yarn_resourcemanager
        ;;
    hadoop-yarn-nm)
        start_yarn_nodemanager $2 $3
        ;;
    hadoop-yarn-proxyserver)
        start_yarn_proxyserver $2 $3
        ;;
    hadoop-mr-historyserver)
        start_mr_historyserver $2 $3
        ;;
    hive-metastore)
        start_hive_metastore $2 $3
        ;;
    hive-hiveserver2)
        start_hive_hiveserver2 $2 $3
        ;;
    hive-svc)
        start_hive_svc $2 $3
        ;;
    flink_yarn_session)
        start_flink_yarn_session $2 $3
        ;;
    dinky_flink17)
        start_dinky_flink17 $2 $3
        ;;
    dinky_flink18)
        start_dinky_flink18 $2 $3
        ;;
    dinky_flink)
        start_dinky_flink $2 $3
        ;;
    *)
        echo "请输入正确的服务启动命令~"
    ;;
esac
